package com.leoman.welfare.controller;

import com.leoman.common.controller.common.GenericEntityController;
import com.leoman.common.factory.DataTableFactory;
import com.leoman.entity.Configue;
import com.leoman.entity.Constant;
import com.leoman.exception.GeneralExceptionHandler;
import com.leoman.sensitivewords.service.SensitiveWordsService;
import com.leoman.utils.ImageUtil;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import com.leoman.welfare.entity.Welfare;
import com.leoman.welfare.service.WelfareService;
import com.leoman.welfare.service.impl.WelfareServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Created by 蔡琦 on 2016/6/29 0029.
 */
@Controller
@RequestMapping(value = "admin/welfare")
public class WelfareController extends GenericEntityController<Welfare, Welfare, WelfareServiceImpl> {

    @Autowired
    private WelfareService welfareService;

    @Autowired
    private SensitiveWordsService sensitiveWordsService;

    /**
     * 列表页面
     *
     * @return path
     */
    @RequestMapping(value = "/index")
    public String index() {
        return "welfare/list";
    }

    /**
     * 查询列表
     */
    @RequestMapping(value = "/list")
    public void list(HttpServletResponse response,
                     Integer draw,
                     Integer start,
                     Integer length) {
        try {

            int pageNum = getPageNum(start, length);

            Page<Welfare> page = welfareService.page(pageNum, length);
            for (Welfare welfare : page.getContent()) {
                if (StringUtils.isNotEmpty(welfare.getCover())) {
                    welfare.setCover(ImageUtil.clearUploadUrl(welfare.getCover()));
                }
            }

            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, emptyData);
        }
    }

    /**
     * 新增
     *
     * @return path
     */
    @RequestMapping("/add")
    public String add() {

        return "welfare/add";
    }

    /**
     * 编辑
     *
     * @return path
     */
    @RequestMapping("/edit")
    public String edit(Long welfareId, Model model) {

        Welfare welfare = welfareService.queryByPK(welfareId);
        if (StringUtils.isNotEmpty(welfare.getCover())) {
            welfare.setCover(ImageUtil.clearUploadUrl(welfare.getCover()));
        }
        model.addAttribute(welfare);

        if (welfare.getType().equals(Constant.WELFARE_TYPE_000)) {

            return "welfare/editMusic";
        } else if (welfare.getType().equals(Constant.WELFARE_TYPE_001)) {

            return "welfare/editExperience";
        } else if (welfare.getType().equals(Constant.WELFARE_TYPE_002)) {

            return "welfare/editEntity";
        } else if (welfare.getType().equals(Constant.WELFARE_TYPE_003)) {

            return "welfare/editEmotion";
        } else if (welfare.getType().equals(Constant.WELFARE_TYPE_004)) {

            return "welfare/editStore";
        } else if (welfare.getType().equals(Constant.WELFARE_TYPE_005)) {

            return "welfare/editGame";
        }
        return null;
    }

    /**
     * 置顶
     */
    @RequestMapping("/stick")
    @ResponseBody
    public Integer stick(Long welfareId) {

        Welfare welfare = welfareService.queryByPK(welfareId);
        welfare.setStatus(1);
        welfareService.save(welfare);
        List<Welfare> welfareList = welfareService.findByStick();
        if (welfareList.size() >= 3) {
            for (int i = 2; i < welfareList.size(); i++) {
                welfareList.get(i).setStatus(0);
                welfareService.save(welfareList.get(i));
            }
            return 1;
        }
        /*List<Welfare> welfareList = welfareService.queryAll();
        for (Welfare welfare1 : welfareList) {
            welfare1.setStatus(0);
            welfareService.save(welfare1);
        }*/


        return 1;
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Long welfareId) {

        return welfareService.deleteWelfare(welfareId);
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    @ResponseBody
    public Integer deleteBatch(String ids) {
        Long[] arrayId = JsonUtil.json2Obj(ids, Long[].class);
        for (Long id : arrayId) {
            welfareService.deleteWelfare(id);
        }
        return 1;
    }

    /**
     * 详情
     *
     * @return path
     */
    @RequestMapping("/detail")
    public String detail(Long welfareId, Model model) {

        Welfare welfare = welfareService.queryByPK(welfareId);
        if (StringUtils.isNotEmpty(welfare.getCover())) {
            welfare.setCover(ImageUtil.clearUploadUrl(welfare.getCover()));
        }
        if (StringUtils.isNotEmpty(welfare.getUrl())) {
            welfare.setUrl(ImageUtil.clearUploadUrl(welfare.getUrl()));
        }
        model.addAttribute(welfare);

        if (welfare.getType().equals(Constant.WELFARE_TYPE_000)) {
            return "welfare/detailMusic";
        } else if (welfare.getType().equals(Constant.WELFARE_TYPE_001)) {

            return "welfare/detailExperience";
        } else if (welfare.getType().equals(Constant.WELFARE_TYPE_002)) {

            return "welfare/detailEntity";
        } else if (welfare.getType().equals(Constant.WELFARE_TYPE_003)) {

            return "welfare/detailEmotion";
        } else if (welfare.getType().equals(Constant.WELFARE_TYPE_004)) {

            return "welfare/detailStore";
        } else if (welfare.getType().equals(Constant.WELFARE_TYPE_005)) {

            return "welfare/detailGame";
        }
        return null;
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Integer save(Long welfareId,
                        String title,
                        String subtitle,
                        Integer type,
                        Integer storeNum,
                        Integer changeNum,
                        String startDate,
                        String endDate,
                        Integer limit,
                        Integer coin,
                        String length,
                        Integer coin1,
                        Integer experience,
                        String description1,
                        Integer coin2,
                        String description2,
                        Integer coin3,
                        String description3,
                        String url4,
                        Integer coin5,
                        String url5,
                        MultipartRequest multipartRequest) throws IOException, ParseException {

        // 过滤敏感字符
        title = sensitiveWordsService.replaceWord(title);
        subtitle = sensitiveWordsService.replaceWord(subtitle);
        length = sensitiveWordsService.replaceWord(length);
        description1 = sensitiveWordsService.replaceWord(description1);
        description2 = sensitiveWordsService.replaceWord(description2);
        description3 = sensitiveWordsService.replaceWord(description3);

        return welfareService.saveAll(welfareId, title, subtitle, type, storeNum, changeNum, startDate, endDate, limit, coin, length, coin1, experience, description1,
                coin2, description2, coin3, description3, url4, coin5, url5, multipartRequest);
    }
}
